package com.cuicui.mall.service;

import com.cuicui.mall.dao.GoodsClassifyDataMapper;
import com.cuicui.mall.entity.GoodsClassifyData;
import com.cuicui.mall.entity.GoodsClassifyDataExample;
import com.cuicui.mall.utils.CommonResponse;
import com.cuicui.mall.vo.GoodsClassifyVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Service
public class GoodsClassifyService {
    @Autowired
    private GoodsClassifyDataMapper goodsClassifyDao;

    public CommonResponse getPage (Integer pageNO, Integer pageSize) {
        GoodsClassifyDataExample goodsClassifyDataExample = new GoodsClassifyDataExample();
        GoodsClassifyDataExample.Criteria criteria = goodsClassifyDataExample.createCriteria();
        PageHelper.startPage(pageNO, pageSize);

        List<GoodsClassifyData> goodsClassifyDataList = goodsClassifyDao.selectByExample(goodsClassifyDataExample);
        List<GoodsClassifyVO> goodsClassifyList = goodsClassifyDataList.stream().map((goodsClassifyDataItem)->{
            GoodsClassifyVO goodsClassifyVO = new GoodsClassifyVO();
            BeanUtils.copyProperties(goodsClassifyDataItem, goodsClassifyVO);
            return goodsClassifyVO;
        }).collect(Collectors.toList());

        PageInfo<GoodsClassifyVO> pageInfo = new PageInfo<>();
        pageInfo.setList(goodsClassifyList);
        return CommonResponse.success(pageInfo);
    }

    public CommonResponse add (GoodsClassifyVO goodsClassifyVO) {
        GoodsClassifyData goodsClassifyData = new GoodsClassifyData();
        BeanUtils.copyProperties(goodsClassifyVO, goodsClassifyData);
        try{
            goodsClassifyDao.insert(goodsClassifyData);
        }catch (Exception e){
            return  CommonResponse.error("500", "添加失败");
        }
        return CommonResponse.success(true);
    }


    public CommonResponse remove (Integer id) {
        try{
            goodsClassifyDao.deleteByPrimaryKey(id);
        }catch (Exception e){
            return CommonResponse.error("500", "删除失败");
        }
        return CommonResponse.success(true);
    }
}
